/*
 * Created on Wed Oct 11 2017
 * Copyright (c) 2017 jianglinjie
 */
'use strict';
import { CaptchaService } from '../../src';
import * as TestConst from './TestConst';
import { CaptchaBusiness } from '../../src';
import { AuthService } from '../../src';
import { StringUtils } from '../../src';
import type { UserInfo } from '../../src';
import { ServiceEventNames } from '../../src';
import { ClientServiceEmitter } from '../../src';

test('Account_Login', async () => {
  const mockCallback = jest.fn();
  let subscription = ClientServiceEmitter.addListener(
    ServiceEventNames.AUTH_SERVICE_ON_LOGIN_STATE_CHANGE,
    mockCallback
  );
  await AuthService.loginWithAccount(
    TestConst.USER_NAME, 
    TestConst.USER_NAME_PWD);
  subscription.remove();

  // The mock function is called one
  expect(mockCallback.mock.calls.length).toBe(1);
  // The first argument of the first call to the function was 0
  expect(mockCallback.mock.calls[0][0]).toBeTruthy();
  
  let token = await AuthService.getToken();
  expect(token).toBeTruthy();

  let curUser: UserInfo = await AuthService.getCurrentUserInfo();
  expect(curUser.uid).toBeTruthy();
  expect(curUser.userName).toMatch(TestConst.USER_NAME);
  expect(curUser.encryptPwd).toMatch(StringUtils.md5(TestConst.USER_NAME_PWD));
});

test('Phone_Login', async () => {
  let captchaResult = await CaptchaService.request({
    phone: TestConst.PHONE,
    business: CaptchaBusiness.LOGIN
  });

  const mockCallback = jest.fn();
  let subscription = ClientServiceEmitter.addListener(
    ServiceEventNames.AUTH_SERVICE_ON_LOGIN_STATE_CHANGE,
    mockCallback
  );
  await AuthService.loginWithPhoneCaptcha(
    TestConst.PHONE, 
    captchaResult.captcha);
    subscription.remove();
    
  // The mock function is called one
  expect(mockCallback.mock.calls.length).toBe(1);
  // The first argument of the first call to the function was 0
  expect(mockCallback.mock.calls[0][0]).toBeTruthy();

  let token = await AuthService.getToken();
  expect(token).toBeTruthy();

  let curUser: UserInfo = await AuthService.getCurrentUserInfo();
  expect(curUser.uid).toBeTruthy();
  expect(curUser.phone).toMatch(TestConst.PHONE);
  expect(curUser.encryptPwd).toMatch(StringUtils.md5(TestConst.PHONE_PWD));
});

test('Auto_Login', async () => {
  await AuthService.loginWithAccount(
    TestConst.USER_NAME, 
    TestConst.USER_NAME_PWD);
  await AuthService.autoLogin();

  let token = await AuthService.getToken();
  expect(token).toBeTruthy();

  let curUser: UserInfo = await AuthService.getCurrentUserInfo();
  expect(curUser.uid).toBeTruthy();
  expect(curUser.userName).toMatch(TestConst.USER_NAME);
  expect(curUser.encryptPwd).toMatch(StringUtils.md5(TestConst.USER_NAME_PWD));

  await AuthService.logout();
  let A = await AuthService.autoLogin();
  
});

test('Logout', async () => {
  await AuthService.loginWithAccount(
    TestConst.USER_NAME, 
    TestConst.USER_NAME_PWD);
  
  let token1 = await AuthService.getToken();
  expect(token1).toBeTruthy();

  await AuthService.logout();
  let token2 = await AuthService.getToken();
  expect(token2).toBeFalsy();
});

// test('Refresh_Token', async () => {
//   await AuthService.loginWithAccount(
//     TestConst.USER_NAME, 
//     TestConst.USER_NAME_PWD);

//   //客户端token 5s过期
//   let timeoutPromise = new Promise((resolve) => {
//     setTimeout(function() {
//       resolve();
//     }, 6000);
//   });

//   await timeoutPromise;

//   let token = await AuthService.getToken();
//   expect(token).toBeTruthy();
// });